package com.lyw.springCloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther liyawei
 * @Date 2022/5/17 16:06
 */
@Slf4j
@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA(){
        return "-------testA";
    }

    @GetMapping("/testB")
    public String testB(){
        return "--------testB";
    }

    @GetMapping("/testD")
    public String testD(){
        /*try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println(">>>>>>>>>>>>>>>>>>> 1111");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 异常比例");

        int age = 10 / 0;
        return "--------testD";
    }

    @GetMapping("/testE")
    public String testE(){

        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 异常数");
        int age = 10 / 0;
        return "--------testE";
    }

    @GetMapping("/testHotKey") //这个代表rest请求地址
    // value = "testHotKeyabc001" testHotKeyabc001 是当前方法的唯一标识
    // blockHandler = "del_testHotKey" 方法出现问题之后的兜底方法， del_testHotKey 兜底方法名称
    @SentinelResource(value = "testHotKey001", blockHandler = "del_testHotKey")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2){

        return "------testHotKey";
    }

    // 兜底方法
    public String del_testHotKey(String p1, String p2, BlockException exception){

        return "自定义提示：del_testHotKeyo(╥﹏╥)o...";
    }
}